var app = require('koa')()
  , logger = require('koa-logger')
  , json = require('koa-json')
  , onerror = require('koa-onerror');
var cors = require('koa-cors');

var tasks = require('./routes/tasks');
var users = require('./routes/users');

app.use(cors({
  origin: '*',
  allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept']
}));




// error handler
onerror(app);

// global middlewares
app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());



app.use(require('koa-static')(__dirname + '/public'));

// routes definition
app.use(tasks.routes(), tasks.allowedMethods());
app.use(users.routes(), users.allowedMethods());

// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
});

module.exports = app;
